package com.example.delivery.controller;

import com.example.delivery.pojo.Restaurant;
import com.example.delivery.service.RestaurantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/restaurant")
public class RestaurantController {

    @Autowired
    private RestaurantService restaurantService;

    @GetMapping("/all")
    public ModelAndView all() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("index");
        modelAndView.addObject("restaurants",restaurantService.findAll());
        return modelAndView;
    }

    /**
     * 根据参观名称搜索
     * @param restaurantName
     * @return
     */
    @GetMapping("/search")
    public ModelAndView search(String restaurantName) {
        ModelAndView modelAndView = new ModelAndView();

        List<Restaurant> restaurants = restaurantService.findByName(restaurantName);
        modelAndView.setViewName("index");
        modelAndView.addObject("restaurants",restaurants);
        modelAndView.addObject("searchName",restaurantName);
        return modelAndView;
    }

    @GetMapping("/{id}")
    public ModelAndView restaurant(@PathVariable("id") Integer id) {
        Restaurant restaurant = restaurantService.findById(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("restaurant",restaurant);
        modelAndView.setViewName("/restaurant");
        return modelAndView;
    }

}
